<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# LANGUAGE ScopedTypeVariables #-}</span><span>
</span><span id="line-4"></span><span class="hs-pragma">{-# LANGUAGE BangPatterns #-}</span><span>
</span><span id="line-5"></span><span>
</span><span id="line-6"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- Module      :  GHC.Foreign</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow, 2008-2011</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- License     :  see libraries/base/LICENSE</span><span>
</span><span id="line-11"></span><span class="hs-comment">--</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Stability   :  internal</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- Portability :  non-portable</span><span>
</span><span id="line-15"></span><span class="hs-comment">--</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- Foreign marshalling support for CStrings with configurable encodings</span><span>
</span><span id="line-17"></span><span class="hs-comment">--</span><span>
</span><span id="line-18"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-19"></span><span>
</span><span id="line-20"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.Foreign</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-21"></span><span>    </span><span class="annot"><span class="hs-comment">-- * C strings with a configurable encoding</span></span><span>
</span><span id="line-22"></span><span>
</span><span id="line-23"></span><span>    </span><span class="hs-comment">-- conversion of C strings into Haskell strings</span><span>
</span><span id="line-24"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-25"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#peekCString"><span class="hs-identifier">peekCString</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-26"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#peekCStringLen"><span class="hs-identifier">peekCStringLen</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-27"></span><span>
</span><span id="line-28"></span><span>    </span><span class="hs-comment">-- conversion of Haskell strings into C strings</span><span>
</span><span id="line-29"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-30"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#newCString"><span class="hs-identifier">newCString</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-31"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#newCStringLen"><span class="hs-identifier">newCStringLen</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-32"></span><span>
</span><span id="line-33"></span><span>    </span><span class="hs-comment">-- conversion of Haskell strings into C strings using temporary storage</span><span>
</span><span id="line-34"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-35"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#withCString"><span class="hs-identifier">withCString</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-36"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#withCStringLen"><span class="hs-identifier">withCStringLen</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-37"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#withCStringsLen"><span class="hs-identifier">withCStringsLen</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-38"></span><span>
</span><span id="line-39"></span><span>    </span><span class="annot"><a href="GHC.Foreign.html#charIsRepresentable"><span class="hs-identifier">charIsRepresentable</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-40"></span><span>  </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-41"></span><span>
</span><span id="line-42"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Foreign.Marshal.Array.html"><span class="hs-identifier">Foreign.Marshal.Array</span></a></span><span>
</span><span id="line-43"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Foreign.C.Types.html"><span class="hs-identifier">Foreign.C.Types</span></a></span><span>
</span><span id="line-44"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Foreign.Ptr.html"><span class="hs-identifier">Foreign.Ptr</span></a></span><span>
</span><span id="line-45"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Foreign.Storable.html"><span class="hs-identifier">Foreign.Storable</span></a></span><span>
</span><span id="line-46"></span><span>
</span><span id="line-47"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Word.html"><span class="hs-identifier">Data.Word</span></a></span><span>
</span><span id="line-48"></span><span>
</span><span id="line-49"></span><span class="hs-comment">-- Imports for the locale-encoding version of marshallers</span><span>
</span><span id="line-50"></span><span>
</span><span id="line-51"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Tuple.html"><span class="hs-identifier">Data.Tuple</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Tuple.html#fst"><span class="hs-identifier">fst</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-52"></span><span>
</span><span id="line-53"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Show.html"><span class="hs-identifier">GHC.Show</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.Show.html#show"><span class="hs-identifier">show</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-54"></span><span>
</span><span id="line-55"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Foreign.Marshal.Alloc.html"><span class="hs-identifier">Foreign.Marshal.Alloc</span></a></span><span>
</span><span id="line-56"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Foreign.ForeignPtr.html"><span class="hs-identifier">Foreign.ForeignPtr</span></a></span><span>
</span><span id="line-57"></span><span>
</span><span id="line-58"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Debug.html#"><span class="hs-identifier">GHC.Debug</span></a></span><span>
</span><span id="line-59"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.List.html"><span class="hs-identifier">GHC.List</span></a></span><span>
</span><span id="line-60"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Num.html"><span class="hs-identifier">GHC.Num</span></a></span><span>
</span><span id="line-61"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Base.html"><span class="hs-identifier">GHC.Base</span></a></span><span>
</span><span id="line-62"></span><span>
</span><span id="line-63"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.IO.html"><span class="hs-identifier">GHC.IO</span></a></span><span>
</span><span id="line-64"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.IO.Exception.html"><span class="hs-identifier">GHC.IO.Exception</span></a></span><span>
</span><span id="line-65"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.IO.Buffer.html"><span class="hs-identifier">GHC.IO.Buffer</span></a></span><span>
</span><span id="line-66"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html"><span class="hs-identifier">GHC.IO.Encoding.Types</span></a></span><span>
</span><span id="line-67"></span><span>
</span><span id="line-68"></span><span>
</span><span id="line-69"></span><span class="annot"><a href="GHC.Foreign.html#c_DEBUG_DUMP"><span class="hs-identifier hs-type">c_DEBUG_DUMP</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span>
</span><span id="line-70"></span><span id="c_DEBUG_DUMP"><span class="annot"><span class="annottext">c_DEBUG_DUMP :: Bool
</span><a href="GHC.Foreign.html#c_DEBUG_DUMP"><span class="hs-identifier hs-var hs-var">c_DEBUG_DUMP</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span>
</span><span id="line-71"></span><span>
</span><span id="line-72"></span><span class="annot"><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-type">putDebugMsg</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-73"></span><span id="putDebugMsg"><span class="annot"><span class="annottext">putDebugMsg :: String -&gt; IO ()
</span><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-var hs-var">putDebugMsg</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Foreign.html#c_DEBUG_DUMP"><span class="hs-identifier hs-var">c_DEBUG_DUMP</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO ()
</span><a href="../../ghc-prim/src/GHC.Debug.html#debugLn"><span class="hs-identifier hs-var">debugLn</span></a></span><span>
</span><span id="line-74"></span><span>            </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO () -&gt; String -&gt; IO ()
forall a b. a -&gt; b -&gt; a
</span><a href="GHC.Base.html#const"><span class="hs-identifier hs-var">const</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-75"></span><span>
</span><span id="line-76"></span><span>
</span><span id="line-77"></span><span class="hs-comment">-- These definitions are identical to those in Foreign.C.String, but copied in here to avoid a cycle:</span><span>
</span><span id="line-78"></span><span class="hs-keyword">type</span><span> </span><span id="CString"><span class="annot"><a href="GHC.Foreign.html#CString"><span class="hs-identifier hs-var">CString</span></a></span></span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="Foreign.C.Types.html#CChar"><span class="hs-identifier hs-type">CChar</span></a></span><span>
</span><span id="line-79"></span><span class="hs-keyword">type</span><span> </span><span id="CStringLen"><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-var">CStringLen</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="Foreign.C.Types.html#CChar"><span class="hs-identifier hs-type">CChar</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-80"></span><span>
</span><span id="line-81"></span><span class="hs-comment">-- exported functions</span><span>
</span><span id="line-82"></span><span class="hs-comment">-- ------------------</span><span>
</span><span id="line-83"></span><span>
</span><span id="line-84"></span><span class="hs-comment">-- | Marshal a NUL terminated C string into a Haskell string.</span><span>
</span><span id="line-85"></span><span class="hs-comment">--</span><span>
</span><span id="line-86"></span><span class="annot"><a href="GHC.Foreign.html#peekCString"><span class="hs-identifier hs-type">peekCString</span></a></span><span>    </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Foreign.html#CString"><span class="hs-identifier hs-type">CString</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span>
</span><span id="line-87"></span><span id="peekCString"><span class="annot"><span class="annottext">peekCString :: TextEncoding -&gt; CString -&gt; IO String
</span><a href="GHC.Foreign.html#peekCString"><span class="hs-identifier hs-var hs-var">peekCString</span></a></span></span><span> </span><span id="local-6989586621679558576"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558576"><span class="hs-identifier hs-var">enc</span></a></span></span><span> </span><span id="local-6989586621679558575"><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558575"><span class="hs-identifier hs-var">cp</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-88"></span><span>    </span><span id="local-6989586621679558574"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558574"><span class="hs-identifier hs-var">sz</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">CChar -&gt; CString -&gt; IO Int
forall a. (Storable a, Eq a) =&gt; a -&gt; Ptr a -&gt; IO Int
</span><a href="Foreign.Marshal.Array.html#lengthArray0"><span class="hs-identifier hs-var">lengthArray0</span></a></span><span> </span><span class="annot"><span class="annottext">CChar
</span><a href="GHC.Foreign.html#nUL"><span class="hs-identifier hs-var">nUL</span></a></span><span> </span><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558575"><span class="hs-identifier hs-var">cp</span></a></span><span>
</span><span id="line-89"></span><span>    </span><span class="annot"><span class="annottext">TextEncoding -&gt; CStringLen -&gt; IO String
</span><a href="GHC.Foreign.html#peekEncodedCString"><span class="hs-identifier hs-var">peekEncodedCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558576"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558575"><span class="hs-identifier hs-var">cp</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558574"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2A"><span class="hs-operator hs-var">*</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="GHC.Foreign.html#cCharSize"><span class="hs-identifier hs-var">cCharSize</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-90"></span><span>
</span><span id="line-91"></span><span class="hs-comment">-- | Marshal a C string with explicit length into a Haskell string.</span><span>
</span><span id="line-92"></span><span class="hs-comment">--</span><span>
</span><span id="line-93"></span><span class="annot"><a href="GHC.Foreign.html#peekCStringLen"><span class="hs-identifier hs-type">peekCStringLen</span></a></span><span>           </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-type">CStringLen</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span>
</span><span id="line-94"></span><span id="peekCStringLen"><span class="annot"><span class="annottext">peekCStringLen :: TextEncoding -&gt; CStringLen -&gt; IO String
</span><a href="GHC.Foreign.html#peekCStringLen"><span class="hs-identifier hs-var hs-var">peekCStringLen</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; CStringLen -&gt; IO String
</span><a href="GHC.Foreign.html#peekEncodedCString"><span class="hs-identifier hs-var">peekEncodedCString</span></a></span><span>
</span><span id="line-95"></span><span>
</span><span id="line-96"></span><span class="hs-comment">-- | Marshal a Haskell string into a NUL terminated C string.</span><span>
</span><span id="line-97"></span><span class="hs-comment">--</span><span>
</span><span id="line-98"></span><span class="hs-comment">-- * the Haskell string may /not/ contain any NUL characters</span><span>
</span><span id="line-99"></span><span class="hs-comment">--</span><span>
</span><span id="line-100"></span><span class="hs-comment">-- * new storage is allocated for the C string and must be</span><span>
</span><span id="line-101"></span><span class="hs-comment">--   explicitly freed using 'Foreign.Marshal.Alloc.free' or</span><span>
</span><span id="line-102"></span><span class="hs-comment">--   'Foreign.Marshal.Alloc.finalizerFree'.</span><span>
</span><span id="line-103"></span><span class="hs-comment">--</span><span>
</span><span id="line-104"></span><span class="annot"><a href="GHC.Foreign.html#newCString"><span class="hs-identifier hs-type">newCString</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="GHC.Foreign.html#CString"><span class="hs-identifier hs-type">CString</span></a></span><span>
</span><span id="line-105"></span><span id="newCString"><span class="annot"><span class="annottext">newCString :: TextEncoding -&gt; String -&gt; IO CString
</span><a href="GHC.Foreign.html#newCString"><span class="hs-identifier hs-var hs-var">newCString</span></a></span></span><span> </span><span id="local-6989586621679558568"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558568"><span class="hs-identifier hs-var">enc</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(CStringLen -&gt; CString) -&gt; IO CStringLen -&gt; IO CString
forall (m :: * -&gt; *) a1 r. Monad m =&gt; (a1 -&gt; r) -&gt; m a1 -&gt; m r
</span><a href="GHC.Base.html#liftM"><span class="hs-identifier hs-var">liftM</span></a></span><span> </span><span class="annot"><span class="annottext">CStringLen -&gt; CString
forall a b. (a, b) -&gt; a
</span><a href="Data.Tuple.html#fst"><span class="hs-identifier hs-var">fst</span></a></span><span> </span><span class="annot"><span class="annottext">(IO CStringLen -&gt; IO CString)
-&gt; (String -&gt; IO CStringLen) -&gt; String -&gt; IO CString
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; Bool -&gt; String -&gt; IO CStringLen
</span><a href="GHC.Foreign.html#newEncodedCString"><span class="hs-identifier hs-var">newEncodedCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558568"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#True"><span class="hs-identifier hs-var">True</span></a></span><span>
</span><span id="line-106"></span><span>
</span><span id="line-107"></span><span class="hs-comment">-- | Marshal a Haskell string into a C string (ie, character array) with</span><span>
</span><span id="line-108"></span><span class="hs-comment">-- explicit length information.</span><span>
</span><span id="line-109"></span><span class="hs-comment">--</span><span>
</span><span id="line-110"></span><span class="hs-comment">-- * new storage is allocated for the C string and must be</span><span>
</span><span id="line-111"></span><span class="hs-comment">--   explicitly freed using 'Foreign.Marshal.Alloc.free' or</span><span>
</span><span id="line-112"></span><span class="hs-comment">--   'Foreign.Marshal.Alloc.finalizerFree'.</span><span>
</span><span id="line-113"></span><span class="hs-comment">--</span><span>
</span><span id="line-114"></span><span class="annot"><a href="GHC.Foreign.html#newCStringLen"><span class="hs-identifier hs-type">newCStringLen</span></a></span><span>     </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-type">CStringLen</span></a></span><span>
</span><span id="line-115"></span><span id="newCStringLen"><span class="annot"><span class="annottext">newCStringLen :: TextEncoding -&gt; String -&gt; IO CStringLen
</span><a href="GHC.Foreign.html#newCStringLen"><span class="hs-identifier hs-var hs-var">newCStringLen</span></a></span></span><span> </span><span id="local-6989586621679558564"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558564"><span class="hs-identifier hs-var">enc</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; Bool -&gt; String -&gt; IO CStringLen
</span><a href="GHC.Foreign.html#newEncodedCString"><span class="hs-identifier hs-var">newEncodedCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558564"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span>
</span><span id="line-116"></span><span>
</span><span id="line-117"></span><span class="hs-comment">-- | Marshal a Haskell string into a NUL terminated C string using temporary</span><span>
</span><span id="line-118"></span><span class="hs-comment">-- storage.</span><span>
</span><span id="line-119"></span><span class="hs-comment">--</span><span>
</span><span id="line-120"></span><span class="hs-comment">-- * the Haskell string may /not/ contain any NUL characters</span><span>
</span><span id="line-121"></span><span class="hs-comment">--</span><span>
</span><span id="line-122"></span><span class="hs-comment">-- * the memory is freed when the subcomputation terminates (either</span><span>
</span><span id="line-123"></span><span class="hs-comment">--   normally or via an exception), so the pointer to the temporary</span><span>
</span><span id="line-124"></span><span class="hs-comment">--   storage must /not/ be used after this.</span><span>
</span><span id="line-125"></span><span class="hs-comment">--</span><span>
</span><span id="line-126"></span><span id="local-6989586621679558694"><span class="annot"><a href="GHC.Foreign.html#withCString"><span class="hs-identifier hs-type">withCString</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Foreign.html#CString"><span class="hs-identifier hs-type">CString</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558694"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558694"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-127"></span><span id="withCString"><span class="annot"><span class="annottext">withCString :: forall a. TextEncoding -&gt; String -&gt; (CString -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withCString"><span class="hs-identifier hs-var hs-var">withCString</span></a></span></span><span> </span><span id="local-6989586621679558563"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558563"><span class="hs-identifier hs-var">enc</span></a></span></span><span> </span><span id="local-6989586621679558562"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558562"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span id="local-6989586621679558561"><span class="annot"><span class="annottext">CString -&gt; IO a
</span><a href="#local-6989586621679558561"><span class="hs-identifier hs-var">act</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; Bool -&gt; String -&gt; (CStringLen -&gt; IO a) -&gt; IO a
forall a.
TextEncoding -&gt; Bool -&gt; String -&gt; (CStringLen -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withEncodedCString"><span class="hs-identifier hs-var">withEncodedCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558563"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#True"><span class="hs-identifier hs-var">True</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558562"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="annot"><span class="annottext">((CStringLen -&gt; IO a) -&gt; IO a) -&gt; (CStringLen -&gt; IO a) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span class="hs-special">(</span><span id="local-6989586621679558559"><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558559"><span class="hs-identifier hs-var">cp</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558558"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558558"><span class="hs-identifier hs-var">_sz</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">CString -&gt; IO a
</span><a href="#local-6989586621679558561"><span class="hs-identifier hs-var">act</span></a></span><span> </span><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558559"><span class="hs-identifier hs-var">cp</span></a></span><span>
</span><span id="line-128"></span><span>
</span><span id="line-129"></span><span class="hs-comment">-- | Marshal a Haskell string into a C string (ie, character array)</span><span>
</span><span id="line-130"></span><span class="hs-comment">-- in temporary storage, with explicit length information.</span><span>
</span><span id="line-131"></span><span class="hs-comment">--</span><span>
</span><span id="line-132"></span><span class="hs-comment">-- * the memory is freed when the subcomputation terminates (either</span><span>
</span><span id="line-133"></span><span class="hs-comment">--   normally or via an exception), so the pointer to the temporary</span><span>
</span><span id="line-134"></span><span class="hs-comment">--   storage must /not/ be used after this.</span><span>
</span><span id="line-135"></span><span class="hs-comment">--</span><span>
</span><span id="line-136"></span><span id="local-6989586621679558688"><span class="annot"><a href="GHC.Foreign.html#withCStringLen"><span class="hs-identifier hs-type">withCStringLen</span></a></span><span>         </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-type">CStringLen</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558688"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558688"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-137"></span><span id="withCStringLen"><span class="annot"><span class="annottext">withCStringLen :: forall a. TextEncoding -&gt; String -&gt; (CStringLen -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withCStringLen"><span class="hs-identifier hs-var hs-var">withCStringLen</span></a></span></span><span> </span><span id="local-6989586621679558557"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558557"><span class="hs-identifier hs-var">enc</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; Bool -&gt; String -&gt; (CStringLen -&gt; IO a) -&gt; IO a
forall a.
TextEncoding -&gt; Bool -&gt; String -&gt; (CStringLen -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withEncodedCString"><span class="hs-identifier hs-var">withEncodedCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558557"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span>
</span><span id="line-138"></span><span>
</span><span id="line-139"></span><span class="hs-comment">-- | Marshal a list of Haskell strings into an array of NUL terminated C strings</span><span>
</span><span id="line-140"></span><span class="hs-comment">-- using temporary storage.</span><span>
</span><span id="line-141"></span><span class="hs-comment">--</span><span>
</span><span id="line-142"></span><span class="hs-comment">-- * the Haskell strings may /not/ contain any NUL characters</span><span>
</span><span id="line-143"></span><span class="hs-comment">--</span><span>
</span><span id="line-144"></span><span class="hs-comment">-- * the memory is freed when the subcomputation terminates (either</span><span>
</span><span id="line-145"></span><span class="hs-comment">--   normally or via an exception), so the pointer to the temporary</span><span>
</span><span id="line-146"></span><span class="hs-comment">--   storage must /not/ be used after this.</span><span>
</span><span id="line-147"></span><span class="hs-comment">--</span><span>
</span><span id="line-148"></span><span id="local-6989586621679558686"><span class="annot"><a href="GHC.Foreign.html#withCStringsLen"><span class="hs-identifier hs-type">withCStringsLen</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span>
</span><span id="line-149"></span><span>                </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-150"></span><span>                </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="GHC.Foreign.html#CString"><span class="hs-identifier hs-type">CString</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558686"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-151"></span><span>                </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558686"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-152"></span><span id="withCStringsLen"><span class="annot"><span class="annottext">withCStringsLen :: forall a.
TextEncoding -&gt; [String] -&gt; (Int -&gt; Ptr CString -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withCStringsLen"><span class="hs-identifier hs-var hs-var">withCStringsLen</span></a></span></span><span> </span><span id="local-6989586621679558556"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558556"><span class="hs-identifier hs-var">enc</span></a></span></span><span> </span><span id="local-6989586621679558555"><span class="annot"><span class="annottext">[String]
</span><a href="#local-6989586621679558555"><span class="hs-identifier hs-var">strs</span></a></span></span><span> </span><span id="local-6989586621679558554"><span class="annot"><span class="annottext">Int -&gt; Ptr CString -&gt; IO a
</span><a href="#local-6989586621679558554"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[CString] -&gt; [String] -&gt; IO a
</span><a href="#local-6989586621679558553"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="annot"><span class="annottext">[String]
</span><a href="#local-6989586621679558555"><span class="hs-identifier hs-var">strs</span></a></span><span>
</span><span id="line-153"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-154"></span><span>  </span><span id="local-6989586621679558553"><span class="annot"><span class="annottext">go :: [CString] -&gt; [String] -&gt; IO a
</span><a href="#local-6989586621679558553"><span class="hs-identifier hs-var hs-var">go</span></a></span></span><span> </span><span id="local-6989586621679558550"><span class="annot"><span class="annottext">[CString]
</span><a href="#local-6989586621679558550"><span class="hs-identifier hs-var">cs</span></a></span></span><span> </span><span class="hs-special">(</span><span id="local-6989586621679558549"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558549"><span class="hs-identifier hs-var">s</span></a></span></span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-type">:</span></a></span><span id="local-6989586621679558548"><span class="annot"><span class="annottext">[String]
</span><a href="#local-6989586621679558548"><span class="hs-identifier hs-var">ss</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; String -&gt; (CString -&gt; IO a) -&gt; IO a
forall a. TextEncoding -&gt; String -&gt; (CString -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withCString"><span class="hs-identifier hs-var">withCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558556"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558549"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="annot"><span class="annottext">((CString -&gt; IO a) -&gt; IO a) -&gt; (CString -&gt; IO a) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558547"><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558547"><span class="hs-identifier hs-var">c</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">[CString] -&gt; [String] -&gt; IO a
</span><a href="#local-6989586621679558553"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558547"><span class="hs-identifier hs-var">c</span></a></span><span class="annot"><span class="annottext">CString -&gt; [CString] -&gt; [CString]
forall a. a -&gt; [a] -&gt; [a]
</span><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-var">:</span></a></span><span class="annot"><span class="annottext">[CString]
</span><a href="#local-6989586621679558550"><span class="hs-identifier hs-var">cs</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[String]
</span><a href="#local-6989586621679558548"><span class="hs-identifier hs-var">ss</span></a></span><span>
</span><span id="line-155"></span><span>  </span><span class="annot"><a href="#local-6989586621679558553"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span id="local-6989586621679558546"><span class="annot"><span class="annottext">[CString]
</span><a href="#local-6989586621679558546"><span class="hs-identifier hs-var">cs</span></a></span></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[CString] -&gt; (Int -&gt; Ptr CString -&gt; IO a) -&gt; IO a
forall a b. Storable a =&gt; [a] -&gt; (Int -&gt; Ptr a -&gt; IO b) -&gt; IO b
</span><a href="Foreign.Marshal.Array.html#withArrayLen"><span class="hs-identifier hs-var">withArrayLen</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[CString] -&gt; [CString]
forall a. [a] -&gt; [a]
</span><a href="GHC.List.html#reverse"><span class="hs-identifier hs-var">reverse</span></a></span><span> </span><span class="annot"><span class="annottext">[CString]
</span><a href="#local-6989586621679558546"><span class="hs-identifier hs-var">cs</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Int -&gt; Ptr CString -&gt; IO a
</span><a href="#local-6989586621679558554"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-156"></span><span>
</span><span id="line-157"></span><span class="hs-comment">-- | Determines whether a character can be accurately encoded in a</span><span>
</span><span id="line-158"></span><span class="hs-comment">-- 'Foreign.C.String.CString'.</span><span>
</span><span id="line-159"></span><span class="hs-comment">--</span><span>
</span><span id="line-160"></span><span class="hs-comment">-- Pretty much anyone who uses this function is in a state of sin because</span><span>
</span><span id="line-161"></span><span class="hs-comment">-- whether or not a character is encodable will, in general, depend on the</span><span>
</span><span id="line-162"></span><span class="hs-comment">-- context in which it occurs.</span><span>
</span><span id="line-163"></span><span class="annot"><a href="GHC.Foreign.html#charIsRepresentable"><span class="hs-identifier hs-type">charIsRepresentable</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Char"><span class="hs-identifier hs-type">Char</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span>
</span><span id="line-164"></span><span class="hs-comment">-- We force enc explicitly because `catch` is lazy in its</span><span>
</span><span id="line-165"></span><span class="hs-comment">-- first argument. We would probably like to force c as well,</span><span>
</span><span id="line-166"></span><span class="hs-comment">-- but unfortunately worker/wrapper produces very bad code for</span><span>
</span><span id="line-167"></span><span class="hs-comment">-- that.</span><span>
</span><span id="line-168"></span><span class="hs-comment">--</span><span>
</span><span id="line-169"></span><span class="hs-comment">-- TODO If this function is performance-critical, it would probably</span><span>
</span><span id="line-170"></span><span class="hs-comment">-- pay to use a single-character specialization of withCString. That</span><span>
</span><span id="line-171"></span><span class="hs-comment">-- would allow worker/wrapper to actually eliminate Char boxes, and</span><span>
</span><span id="line-172"></span><span class="hs-comment">-- would also get rid of the completely unnecessary cons allocation.</span><span>
</span><span id="line-173"></span><span id="charIsRepresentable"><span class="annot"><span class="annottext">charIsRepresentable :: TextEncoding -&gt; Char -&gt; IO Bool
</span><a href="GHC.Foreign.html#charIsRepresentable"><span class="hs-identifier hs-var hs-var">charIsRepresentable</span></a></span></span><span> </span><span class="hs-glyph">!</span><span id="local-6989586621679558543"><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558543"><span class="hs-identifier hs-var">enc</span></a></span></span><span> </span><span id="local-6989586621679558542"><span class="annot"><span class="annottext">Char
</span><a href="#local-6989586621679558542"><span class="hs-identifier hs-var">c</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-174"></span><span>  </span><span class="annot"><span class="annottext">TextEncoding -&gt; String -&gt; (CString -&gt; IO Bool) -&gt; IO Bool
forall a. TextEncoding -&gt; String -&gt; (CString -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withCString"><span class="hs-identifier hs-var">withCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558543"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">Char
</span><a href="#local-6989586621679558542"><span class="hs-identifier hs-var">c</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-175"></span><span>              </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679558541"><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558541"><span class="hs-identifier hs-var">cstr</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span> </span><span id="local-6989586621679558540"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558540"><span class="hs-identifier hs-var">str</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TextEncoding -&gt; CString -&gt; IO String
</span><a href="GHC.Foreign.html#peekCString"><span class="hs-identifier hs-var">peekCString</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoding
</span><a href="#local-6989586621679558543"><span class="hs-identifier hs-var">enc</span></a></span><span> </span><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558541"><span class="hs-identifier hs-var">cstr</span></a></span><span>
</span><span id="line-176"></span><span>                           </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558540"><span class="hs-identifier hs-var">str</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-177"></span><span>                             </span><span class="hs-special">[</span><span id="local-6989586621679558539"><span class="annot"><span class="annottext">Char
</span><a href="#local-6989586621679558539"><span class="hs-identifier hs-var">ch</span></a></span></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Char
</span><a href="#local-6989586621679558539"><span class="hs-identifier hs-var">ch</span></a></span><span> </span><span class="annot"><span class="annottext">Char -&gt; Char -&gt; Bool
forall a. Eq a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3D%3D"><span class="hs-operator hs-var">==</span></a></span><span> </span><span class="annot"><span class="annottext">Char
</span><a href="#local-6989586621679558542"><span class="hs-identifier hs-var">c</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; IO Bool
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#True"><span class="hs-identifier hs-var">True</span></a></span><span>
</span><span id="line-178"></span><span>                             </span><span class="annot"><span class="annottext">String
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; IO Bool
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-179"></span><span>    </span><span class="annot"><span class="annottext">IO Bool -&gt; (IOException -&gt; IO Bool) -&gt; IO Bool
forall e a. Exception e =&gt; IO a -&gt; (e -&gt; IO a) -&gt; IO a
</span><a href="GHC.IO.html#catch"><span class="hs-operator hs-var">`catch`</span></a></span><span>
</span><span id="line-180"></span><span>       </span><span class="hs-glyph">\</span><span class="hs-special">(</span><span class="annot"><span class="annottext">IOException
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Exception.html#IOException"><span class="hs-identifier hs-type">IOException</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; IO Bool
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span>
</span><span id="line-181"></span><span>
</span><span id="line-182"></span><span class="hs-comment">-- auxiliary definitions</span><span>
</span><span id="line-183"></span><span class="hs-comment">-- ----------------------</span><span>
</span><span id="line-184"></span><span>
</span><span id="line-185"></span><span class="hs-comment">-- C's end of string character</span><span>
</span><span id="line-186"></span><span class="annot"><a href="GHC.Foreign.html#nUL"><span class="hs-identifier hs-type">nUL</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Foreign.C.Types.html#CChar"><span class="hs-identifier hs-type">CChar</span></a></span><span>
</span><span id="line-187"></span><span id="nUL"><span class="annot"><span class="annottext">nUL :: CChar
</span><a href="GHC.Foreign.html#nUL"><span class="hs-identifier hs-var hs-var">nUL</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">CChar
</span><span class="hs-number">0</span></span><span>
</span><span id="line-188"></span><span>
</span><span id="line-189"></span><span class="hs-comment">-- Size of a CChar in bytes</span><span>
</span><span id="line-190"></span><span class="annot"><a href="GHC.Foreign.html#cCharSize"><span class="hs-identifier hs-type">cCharSize</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span>
</span><span id="line-191"></span><span id="cCharSize"><span class="annot"><span class="annottext">cCharSize :: Int
</span><a href="GHC.Foreign.html#cCharSize"><span class="hs-identifier hs-var hs-var">cCharSize</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">CChar -&gt; Int
forall a. Storable a =&gt; a -&gt; Int
</span><a href="Foreign.Storable.html#sizeOf"><span class="hs-identifier hs-var">sizeOf</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CChar
forall a. HasCallStack =&gt; a
</span><a href="GHC.Err.html#undefined"><span class="hs-identifier hs-var">undefined</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Foreign.C.Types.html#CChar"><span class="hs-identifier hs-type">CChar</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-192"></span><span>
</span><span id="line-193"></span><span>
</span><span id="line-194"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="GHC.Foreign.html#peekEncodedCString"><span class="hs-pragma hs-type">peekEncodedCString</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-195"></span><span class="annot"><a href="GHC.Foreign.html#peekEncodedCString"><span class="hs-identifier hs-type">peekEncodedCString</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-comment">-- ^ Encoding of CString</span><span>
</span><span id="line-196"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-type">CStringLen</span></a></span><span>
</span><span id="line-197"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span>    </span><span class="hs-comment">-- ^ String in Haskell terms</span><span>
</span><span id="line-198"></span><span id="peekEncodedCString"><span class="annot"><span class="annottext">peekEncodedCString :: TextEncoding -&gt; CStringLen -&gt; IO String
</span><a href="GHC.Foreign.html#peekEncodedCString"><span class="hs-identifier hs-var hs-var">peekEncodedCString</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">mkTextDecoder :: ()
</span><a href="GHC.IO.Encoding.Types.html#mkTextDecoder"><span class="hs-identifier hs-var">mkTextDecoder</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621679558526"><span class="annot"><span class="annottext">IO (TextDecoder dstate)
</span><a href="#local-6989586621679558526"><span class="hs-identifier hs-var">mk_decoder</span></a></span></span><span> </span><span class="hs-special">}</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span id="local-6989586621679558525"><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558525"><span class="hs-identifier hs-var">p</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558524"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558524"><span class="hs-identifier hs-var">sz_bytes</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-199"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO (TextDecoder dstate)
-&gt; (TextDecoder dstate -&gt; IO ())
-&gt; (TextDecoder dstate -&gt; IO String)
-&gt; IO String
forall a b c. IO a -&gt; (a -&gt; IO b) -&gt; (a -&gt; IO c) -&gt; IO c
</span><a href="GHC.IO.html#bracket"><span class="hs-identifier hs-var">bracket</span></a></span><span> </span><span class="annot"><span class="annottext">IO (TextDecoder dstate)
</span><a href="#local-6989586621679558526"><span class="hs-identifier hs-var">mk_decoder</span></a></span><span> </span><span class="annot"><span class="annottext">TextDecoder dstate -&gt; IO ()
forall from to state. BufferCodec from to state -&gt; IO ()
</span><a href="GHC.IO.Encoding.Types.html#close"><span class="hs-identifier hs-var hs-var">close</span></a></span><span> </span><span class="annot"><span class="annottext">((TextDecoder dstate -&gt; IO String) -&gt; IO String)
-&gt; (TextDecoder dstate -&gt; IO String) -&gt; IO String
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558521"><span class="annot"><span class="annottext">TextDecoder dstate
</span><a href="#local-6989586621679558521"><span class="hs-identifier hs-var">decoder</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-200"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679558517"><span class="annot"><span class="annottext">chunk_size :: Int
</span><a href="#local-6989586621679558517"><span class="hs-identifier hs-var hs-var">chunk_size</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558524"><span class="hs-identifier hs-var">sz_bytes</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Ord a =&gt; a -&gt; a -&gt; a
</span><a href="../../ghc-prim/src/GHC.Classes.html#max"><span class="hs-operator hs-var">`max`</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">1</span></span><span> </span><span class="hs-comment">-- Decode buffer chunk size in characters: one iteration only for ASCII</span><span>
</span><span id="line-201"></span><span>      </span><span id="local-6989586621679558515"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558515"><span class="hs-identifier hs-var">from0</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(RawBuffer Word8 -&gt; Buffer Word8)
-&gt; IO (RawBuffer Word8) -&gt; IO (Buffer Word8)
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679558514"><span class="annot"><span class="annottext">RawBuffer Word8
</span><a href="#local-6989586621679558514"><span class="hs-identifier hs-var">fp</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Int -&gt; Buffer Word8 -&gt; Buffer Word8
forall e. Int -&gt; Buffer e -&gt; Buffer e
</span><a href="GHC.IO.Buffer.html#bufferAdd"><span class="hs-identifier hs-var">bufferAdd</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558524"><span class="hs-identifier hs-var">sz_bytes</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">RawBuffer Word8 -&gt; Int -&gt; BufferState -&gt; Buffer Word8
forall e. RawBuffer e -&gt; Int -&gt; BufferState -&gt; Buffer e
</span><a href="GHC.IO.Buffer.html#emptyBuffer"><span class="hs-identifier hs-var">emptyBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">RawBuffer Word8
</span><a href="#local-6989586621679558514"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558524"><span class="hs-identifier hs-var">sz_bytes</span></a></span><span> </span><span class="annot"><span class="annottext">BufferState
</span><a href="GHC.IO.Buffer.html#ReadBuffer"><span class="hs-identifier hs-var">ReadBuffer</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">(IO (RawBuffer Word8) -&gt; IO (Buffer Word8))
-&gt; IO (RawBuffer Word8) -&gt; IO (Buffer Word8)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8 -&gt; IO (RawBuffer Word8)
forall a. Ptr a -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier hs-var">newForeignPtr_</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CString -&gt; Ptr Word8
forall a b. Ptr a -&gt; Ptr b
</span><a href="GHC.Ptr.html#castPtr"><span class="hs-identifier hs-var">castPtr</span></a></span><span> </span><span class="annot"><span class="annottext">CString
</span><a href="#local-6989586621679558525"><span class="hs-identifier hs-var">p</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-202"></span><span>      </span><span id="local-6989586621679558508"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558508"><span class="hs-identifier hs-var">to</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Int -&gt; BufferState -&gt; IO CharBuffer
</span><a href="GHC.IO.Buffer.html#newCharBuffer"><span class="hs-identifier hs-var">newCharBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558517"><span class="hs-identifier hs-var">chunk_size</span></a></span><span> </span><span class="annot"><span class="annottext">BufferState
</span><a href="GHC.IO.Buffer.html#WriteBuffer"><span class="hs-identifier hs-var">WriteBuffer</span></a></span><span>
</span><span id="line-203"></span><span>
</span><span id="line-204"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679558487"><span class="annot"><span class="annottext">go :: t -&gt; Buffer Word8 -&gt; IO String
</span><a href="#local-6989586621679558487"><span class="hs-identifier hs-var hs-var">go</span></a></span></span><span> </span><span class="hs-glyph">!</span><span id="local-6989586621679558486"><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558486"><span class="hs-identifier hs-var">iteration</span></a></span></span><span> </span><span id="local-6989586621679558485"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558485"><span class="hs-identifier hs-var">from</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-205"></span><span>            </span><span class="hs-special">(</span><span id="local-6989586621679558484"><span class="annot"><span class="annottext">CodingProgress
</span><a href="#local-6989586621679558484"><span class="hs-identifier hs-var">why</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558483"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558483"><span class="hs-identifier hs-var">from'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558482"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558482"><span class="hs-identifier hs-var">to'</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TextDecoder dstate -&gt; CodeBuffer Word8 Char
forall from to state.
BufferCodec from to state -&gt; CodeBuffer from to
</span><a href="GHC.IO.Encoding.Types.html#encode"><span class="hs-identifier hs-var hs-var">encode</span></a></span><span> </span><span class="annot"><span class="annottext">TextDecoder dstate
</span><a href="#local-6989586621679558521"><span class="hs-identifier hs-var">decoder</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558485"><span class="hs-identifier hs-var">from</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558508"><span class="hs-identifier hs-var">to</span></a></span><span>
</span><span id="line-206"></span><span>            </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; Bool
forall e. Buffer e -&gt; Bool
</span><a href="GHC.IO.Buffer.html#isEmptyBuffer"><span class="hs-identifier hs-var">isEmptyBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558483"><span class="hs-identifier hs-var">from'</span></a></span><span>
</span><span id="line-207"></span><span>             </span><span class="hs-keyword">then</span><span>
</span><span id="line-208"></span><span>              </span><span class="hs-comment">-- No input remaining: @why@ will be InputUnderflow, but we don't care</span><span>
</span><span id="line-209"></span><span>              </span><span class="annot"><span class="annottext">CharBuffer -&gt; (Ptr Char -&gt; IO String) -&gt; IO String
forall e a. Buffer e -&gt; (Ptr e -&gt; IO a) -&gt; IO a
</span><a href="GHC.IO.Buffer.html#withBuffer"><span class="hs-identifier hs-var">withBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558482"><span class="hs-identifier hs-var">to'</span></a></span><span> </span><span class="annot"><span class="annottext">((Ptr Char -&gt; IO String) -&gt; IO String)
-&gt; (Ptr Char -&gt; IO String) -&gt; IO String
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Ptr Char -&gt; IO String
forall a. Storable a =&gt; Int -&gt; Ptr a -&gt; IO [a]
</span><a href="Foreign.Marshal.Array.html#peekArray"><span class="hs-identifier hs-var">peekArray</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CharBuffer -&gt; Int
forall e. Buffer e -&gt; Int
</span><a href="GHC.IO.Buffer.html#bufferElems"><span class="hs-identifier hs-var">bufferElems</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558482"><span class="hs-identifier hs-var">to'</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-210"></span><span>             </span><span class="hs-keyword">else</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-211"></span><span>              </span><span class="hs-comment">-- Input remaining: what went wrong?</span><span>
</span><span id="line-212"></span><span>              </span><span class="annot"><span class="annottext">String -&gt; IO ()
</span><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-var">putDebugMsg</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;peekEncodedCString: &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; String
forall a. Show a =&gt; a -&gt; String
</span><a href="GHC.Show.html#show"><span class="hs-identifier hs-var">show</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558486"><span class="hs-identifier hs-var">iteration</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">CodingProgress -&gt; String
forall a. Show a =&gt; a -&gt; String
</span><a href="GHC.Show.html#show"><span class="hs-identifier hs-var">show</span></a></span><span> </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="#local-6989586621679558484"><span class="hs-identifier hs-var">why</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-213"></span><span>              </span><span class="hs-special">(</span><span id="local-6989586621679558476"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558476"><span class="hs-identifier hs-var">from''</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558475"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558475"><span class="hs-identifier hs-var">to''</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="#local-6989586621679558484"><span class="hs-identifier hs-var">why</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="GHC.IO.Encoding.Types.html#InvalidSequence"><span class="hs-identifier hs-var">InvalidSequence</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">TextDecoder dstate
-&gt; Buffer Word8 -&gt; CharBuffer -&gt; IO (Buffer Word8, CharBuffer)
forall from to state.
BufferCodec from to state
-&gt; Buffer from -&gt; Buffer to -&gt; IO (Buffer from, Buffer to)
</span><a href="GHC.IO.Encoding.Types.html#recover"><span class="hs-identifier hs-var hs-var">recover</span></a></span><span> </span><span class="annot"><span class="annottext">TextDecoder dstate
</span><a href="#local-6989586621679558521"><span class="hs-identifier hs-var">decoder</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558483"><span class="hs-identifier hs-var">from'</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558482"><span class="hs-identifier hs-var">to'</span></a></span><span> </span><span class="hs-comment">-- These conditions are equally bad because</span><span>
</span><span id="line-214"></span><span>                                            </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="GHC.IO.Encoding.Types.html#InputUnderflow"><span class="hs-identifier hs-var">InputUnderflow</span></a></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">TextDecoder dstate
-&gt; Buffer Word8 -&gt; CharBuffer -&gt; IO (Buffer Word8, CharBuffer)
forall from to state.
BufferCodec from to state
-&gt; Buffer from -&gt; Buffer to -&gt; IO (Buffer from, Buffer to)
</span><a href="GHC.IO.Encoding.Types.html#recover"><span class="hs-identifier hs-var hs-var">recover</span></a></span><span> </span><span class="annot"><span class="annottext">TextDecoder dstate
</span><a href="#local-6989586621679558521"><span class="hs-identifier hs-var">decoder</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558483"><span class="hs-identifier hs-var">from'</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558482"><span class="hs-identifier hs-var">to'</span></a></span><span> </span><span class="hs-comment">-- they indicate malformed/truncated input</span><span>
</span><span id="line-215"></span><span>                                            </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="GHC.IO.Encoding.Types.html#OutputUnderflow"><span class="hs-identifier hs-var">OutputUnderflow</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">(Buffer Word8, CharBuffer) -&gt; IO (Buffer Word8, CharBuffer)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558483"><span class="hs-identifier hs-var">from'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558482"><span class="hs-identifier hs-var">to'</span></a></span><span class="hs-special">)</span><span>       </span><span class="hs-comment">-- We will have more space next time round</span><span>
</span><span id="line-216"></span><span>              </span><span class="annot"><span class="annottext">String -&gt; IO ()
</span><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-var">putDebugMsg</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;peekEncodedCString: from &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558485"><span class="hs-identifier hs-var">from</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558483"><span class="hs-identifier hs-var">from'</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558476"><span class="hs-identifier hs-var">from''</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-217"></span><span>              </span><span class="annot"><span class="annottext">String -&gt; IO ()
</span><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-var">putDebugMsg</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;peekEncodedCString: to &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558508"><span class="hs-identifier hs-var">to</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558482"><span class="hs-identifier hs-var">to'</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558475"><span class="hs-identifier hs-var">to''</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-218"></span><span>              </span><span id="local-6989586621679558469"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558469"><span class="hs-identifier hs-var">to_chars</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">CharBuffer -&gt; (Ptr Char -&gt; IO String) -&gt; IO String
forall e a. Buffer e -&gt; (Ptr e -&gt; IO a) -&gt; IO a
</span><a href="GHC.IO.Buffer.html#withBuffer"><span class="hs-identifier hs-var">withBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558475"><span class="hs-identifier hs-var">to''</span></a></span><span> </span><span class="annot"><span class="annottext">((Ptr Char -&gt; IO String) -&gt; IO String)
-&gt; (Ptr Char -&gt; IO String) -&gt; IO String
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Ptr Char -&gt; IO String
forall a. Storable a =&gt; Int -&gt; Ptr a -&gt; IO [a]
</span><a href="Foreign.Marshal.Array.html#peekArray"><span class="hs-identifier hs-var">peekArray</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CharBuffer -&gt; Int
forall e. Buffer e -&gt; Int
</span><a href="GHC.IO.Buffer.html#bufferElems"><span class="hs-identifier hs-var">bufferElems</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558475"><span class="hs-identifier hs-var">to''</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-219"></span><span>              </span><span class="annot"><span class="annottext">(String -&gt; String) -&gt; IO String -&gt; IO String
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558469"><span class="hs-identifier hs-var">to_chars</span></a></span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">(IO String -&gt; IO String) -&gt; IO String -&gt; IO String
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; Buffer Word8 -&gt; IO String
</span><a href="#local-6989586621679558487"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558486"><span class="hs-identifier hs-var">iteration</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; t -&gt; t
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558476"><span class="hs-identifier hs-var">from''</span></a></span><span>
</span><span id="line-220"></span><span>
</span><span id="line-221"></span><span>      </span><span class="annot"><span class="annottext">Int -&gt; Buffer Word8 -&gt; IO String
forall {t}. (Show t, Num t) =&gt; t -&gt; Buffer Word8 -&gt; IO String
</span><a href="#local-6989586621679558487"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558515"><span class="hs-identifier hs-var">from0</span></a></span><span>
</span><span id="line-222"></span><span>
</span><span id="line-223"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="GHC.Foreign.html#withEncodedCString"><span class="hs-pragma hs-type">withEncodedCString</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-224"></span><span id="local-6989586621679558692"><span class="annot"><a href="GHC.Foreign.html#withEncodedCString"><span class="hs-identifier hs-type">withEncodedCString</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span>         </span><span class="hs-comment">-- ^ Encoding of CString to create</span><span>
</span><span id="line-225"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span>                 </span><span class="hs-comment">-- ^ Null-terminate?</span><span>
</span><span id="line-226"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span>               </span><span class="hs-comment">-- ^ String to encode</span><span>
</span><span id="line-227"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-type">CStringLen</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558692"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-comment">-- ^ Worker that can safely use the allocated memory</span><span>
</span><span id="line-228"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558692"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-229"></span><span id="withEncodedCString"><span class="annot"><span class="annottext">withEncodedCString :: forall a.
TextEncoding -&gt; Bool -&gt; String -&gt; (CStringLen -&gt; IO a) -&gt; IO a
</span><a href="GHC.Foreign.html#withEncodedCString"><span class="hs-identifier hs-var hs-var">withEncodedCString</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">mkTextEncoder :: ()
</span><a href="GHC.IO.Encoding.Types.html#mkTextEncoder"><span class="hs-identifier hs-var">mkTextEncoder</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621679558458"><span class="annot"><span class="annottext">IO (TextEncoder estate)
</span><a href="#local-6989586621679558458"><span class="hs-identifier hs-var">mk_encoder</span></a></span></span><span> </span><span class="hs-special">}</span><span class="hs-special">)</span><span> </span><span id="local-6989586621679558457"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558457"><span class="hs-identifier hs-var">null_terminate</span></a></span></span><span> </span><span id="local-6989586621679558456"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558456"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span id="local-6989586621679558455"><span class="annot"><span class="annottext">CStringLen -&gt; IO a
</span><a href="#local-6989586621679558455"><span class="hs-identifier hs-var">act</span></a></span></span><span>
</span><span id="line-230"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO (TextEncoder estate)
-&gt; (TextEncoder estate -&gt; IO ())
-&gt; (TextEncoder estate -&gt; IO a)
-&gt; IO a
forall a b c. IO a -&gt; (a -&gt; IO b) -&gt; (a -&gt; IO c) -&gt; IO c
</span><a href="GHC.IO.html#bracket"><span class="hs-identifier hs-var">bracket</span></a></span><span> </span><span class="annot"><span class="annottext">IO (TextEncoder estate)
</span><a href="#local-6989586621679558458"><span class="hs-identifier hs-var">mk_encoder</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoder estate -&gt; IO ()
forall from to state. BufferCodec from to state -&gt; IO ()
</span><a href="GHC.IO.Encoding.Types.html#close"><span class="hs-identifier hs-var hs-var">close</span></a></span><span> </span><span class="annot"><span class="annottext">((TextEncoder estate -&gt; IO a) -&gt; IO a)
-&gt; (TextEncoder estate -&gt; IO a) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558454"><span class="annot"><span class="annottext">TextEncoder estate
</span><a href="#local-6989586621679558454"><span class="hs-identifier hs-var">encoder</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">String -&gt; (Int -&gt; Ptr Char -&gt; IO a) -&gt; IO a
forall a b. Storable a =&gt; [a] -&gt; (Int -&gt; Ptr a -&gt; IO b) -&gt; IO b
</span><a href="Foreign.Marshal.Array.html#withArrayLen"><span class="hs-identifier hs-var">withArrayLen</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558456"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="annot"><span class="annottext">((Int -&gt; Ptr Char -&gt; IO a) -&gt; IO a)
-&gt; (Int -&gt; Ptr Char -&gt; IO a) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558453"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558453"><span class="hs-identifier hs-var">sz</span></a></span></span><span> </span><span id="local-6989586621679558452"><span class="annot"><span class="annottext">Ptr Char
</span><a href="#local-6989586621679558452"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-231"></span><span>      </span><span id="local-6989586621679558451"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558451"><span class="hs-identifier hs-var">from</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(RawBuffer Char -&gt; CharBuffer)
-&gt; IO (RawBuffer Char) -&gt; IO CharBuffer
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679558450"><span class="annot"><span class="annottext">RawBuffer Char
</span><a href="#local-6989586621679558450"><span class="hs-identifier hs-var">fp</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Int -&gt; CharBuffer -&gt; CharBuffer
forall e. Int -&gt; Buffer e -&gt; Buffer e
</span><a href="GHC.IO.Buffer.html#bufferAdd"><span class="hs-identifier hs-var">bufferAdd</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558453"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">RawBuffer Char -&gt; Int -&gt; BufferState -&gt; CharBuffer
forall e. RawBuffer e -&gt; Int -&gt; BufferState -&gt; Buffer e
</span><a href="GHC.IO.Buffer.html#emptyBuffer"><span class="hs-identifier hs-var">emptyBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">RawBuffer Char
</span><a href="#local-6989586621679558450"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558453"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="annot"><span class="annottext">BufferState
</span><a href="GHC.IO.Buffer.html#ReadBuffer"><span class="hs-identifier hs-var">ReadBuffer</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">(IO (RawBuffer Char) -&gt; IO CharBuffer)
-&gt; IO (RawBuffer Char) -&gt; IO CharBuffer
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Char -&gt; IO (RawBuffer Char)
forall a. Ptr a -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier hs-var">newForeignPtr_</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Char
</span><a href="#local-6989586621679558452"><span class="hs-identifier hs-var">p</span></a></span><span>
</span><span id="line-232"></span><span>
</span><span id="line-233"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679558440"><span class="annot"><span class="annottext">go :: t -&gt; Int -&gt; IO a
</span><a href="#local-6989586621679558440"><span class="hs-identifier hs-var hs-var">go</span></a></span></span><span> </span><span class="hs-glyph">!</span><span id="local-6989586621679558439"><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558439"><span class="hs-identifier hs-var">iteration</span></a></span></span><span> </span><span id="local-6989586621679558438"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558438"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-234"></span><span>           </span><span class="annot"><span class="annottext">String -&gt; IO ()
</span><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-var">putDebugMsg</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;withEncodedCString: &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; String
forall a. Show a =&gt; a -&gt; String
</span><a href="GHC.Show.html#show"><span class="hs-identifier hs-var">show</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558439"><span class="hs-identifier hs-var">iteration</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-235"></span><span>           </span><span class="annot"><span class="annottext">Int -&gt; (Ptr Word8 -&gt; IO a) -&gt; IO a
forall a b. Int -&gt; (Ptr a -&gt; IO b) -&gt; IO b
</span><a href="Foreign.Marshal.Alloc.html#allocaBytes"><span class="hs-identifier hs-var">allocaBytes</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558438"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span> </span><span class="annot"><span class="annottext">((Ptr Word8 -&gt; IO a) -&gt; IO a) -&gt; (Ptr Word8 -&gt; IO a) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558436"><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558436"><span class="hs-identifier hs-var">to_p</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-236"></span><span>            </span><span class="hs-comment">-- See Note [Check *before* fill in withEncodedCString] about why</span><span>
</span><span id="line-237"></span><span>            </span><span class="hs-comment">-- this is subtle.</span><span>
</span><span id="line-238"></span><span>            </span><span id="local-6989586621679558435"><span class="annot"><span class="annottext">Maybe (Buffer Word8)
</span><a href="#local-6989586621679558435"><span class="hs-identifier hs-var">mb_res</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TextEncoder estate
-&gt; Bool
-&gt; CharBuffer
-&gt; Ptr Word8
-&gt; Int
-&gt; IO (Maybe (Buffer Word8))
forall dstate.
TextEncoder dstate
-&gt; Bool
-&gt; CharBuffer
-&gt; Ptr Word8
-&gt; Int
-&gt; IO (Maybe (Buffer Word8))
</span><a href="GHC.Foreign.html#tryFillBuffer"><span class="hs-identifier hs-var">tryFillBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoder estate
</span><a href="#local-6989586621679558454"><span class="hs-identifier hs-var">encoder</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558457"><span class="hs-identifier hs-var">null_terminate</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558451"><span class="hs-identifier hs-var">from</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558436"><span class="hs-identifier hs-var">to_p</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558438"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span>
</span><span id="line-239"></span><span>            </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Maybe (Buffer Word8)
</span><a href="#local-6989586621679558435"><span class="hs-identifier hs-var">mb_res</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-240"></span><span>              </span><span class="annot"><span class="annottext">Maybe (Buffer Word8)
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">t -&gt; Int -&gt; IO a
</span><a href="#local-6989586621679558440"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558439"><span class="hs-identifier hs-var">iteration</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; t -&gt; t
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558438"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2A"><span class="hs-operator hs-var">*</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">2</span></span><span class="hs-special">)</span><span>
</span><span id="line-241"></span><span>              </span><span class="annot"><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-type">Just</span></a></span><span> </span><span id="local-6989586621679558433"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558433"><span class="hs-identifier hs-var">to_buf</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; Bool -&gt; (CStringLen -&gt; IO a) -&gt; IO a
forall r. Buffer Word8 -&gt; Bool -&gt; (CStringLen -&gt; IO r) -&gt; IO r
</span><a href="GHC.Foreign.html#withCStringBuffer"><span class="hs-identifier hs-var">withCStringBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558433"><span class="hs-identifier hs-var">to_buf</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558457"><span class="hs-identifier hs-var">null_terminate</span></a></span><span> </span><span class="annot"><span class="annottext">CStringLen -&gt; IO a
</span><a href="#local-6989586621679558455"><span class="hs-identifier hs-var">act</span></a></span><span>
</span><span id="line-242"></span><span>
</span><span id="line-243"></span><span>      </span><span class="hs-comment">-- If the input string is ASCII, this value will ensure we only allocate once</span><span>
</span><span id="line-244"></span><span>      </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; IO a
forall {t}. (Show t, Num t) =&gt; t -&gt; Int -&gt; IO a
</span><a href="#local-6989586621679558440"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="GHC.Foreign.html#cCharSize"><span class="hs-identifier hs-var">cCharSize</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2A"><span class="hs-operator hs-var">*</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558453"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-245"></span><span>
</span><span id="line-246"></span><span id="local-6989586621679558624"><span class="annot"><a href="GHC.Foreign.html#withCStringBuffer"><span class="hs-identifier hs-type">withCStringBuffer</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Buffer.html#Buffer"><span class="hs-identifier hs-type">Buffer</span></a></span><span> </span><span class="annot"><a href="GHC.Word.html#Word8"><span class="hs-identifier hs-type">Word8</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-type">CStringLen</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558624"><span class="hs-identifier hs-type">r</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558624"><span class="hs-identifier hs-type">r</span></a></span></span><span>
</span><span id="line-247"></span><span id="withCStringBuffer"><span class="annot"><span class="annottext">withCStringBuffer :: forall r. Buffer Word8 -&gt; Bool -&gt; (CStringLen -&gt; IO r) -&gt; IO r
</span><a href="GHC.Foreign.html#withCStringBuffer"><span class="hs-identifier hs-var hs-var">withCStringBuffer</span></a></span></span><span> </span><span id="local-6989586621679558425"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558425"><span class="hs-identifier hs-var">to_buf</span></a></span></span><span> </span><span id="local-6989586621679558424"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558424"><span class="hs-identifier hs-var">null_terminate</span></a></span></span><span> </span><span id="local-6989586621679558423"><span class="annot"><span class="annottext">CStringLen -&gt; IO r
</span><a href="#local-6989586621679558423"><span class="hs-identifier hs-var">act</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-248"></span><span>  </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679558422"><span class="annot"><span class="annottext">bytes :: Int
</span><a href="#local-6989586621679558422"><span class="hs-identifier hs-var hs-var">bytes</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; Int
forall e. Buffer e -&gt; Int
</span><a href="GHC.IO.Buffer.html#bufferElems"><span class="hs-identifier hs-var">bufferElems</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558425"><span class="hs-identifier hs-var">to_buf</span></a></span><span>
</span><span id="line-249"></span><span>  </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; (Ptr Word8 -&gt; IO r) -&gt; IO r
forall e a. Buffer e -&gt; (Ptr e -&gt; IO a) -&gt; IO a
</span><a href="GHC.IO.Buffer.html#withBuffer"><span class="hs-identifier hs-var">withBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558425"><span class="hs-identifier hs-var">to_buf</span></a></span><span> </span><span class="annot"><span class="annottext">((Ptr Word8 -&gt; IO r) -&gt; IO r) -&gt; (Ptr Word8 -&gt; IO r) -&gt; IO r
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558421"><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558421"><span class="hs-identifier hs-var">to_ptr</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-250"></span><span>    </span><span class="annot"><span class="annottext">Bool -&gt; IO () -&gt; IO ()
forall (f :: * -&gt; *). Applicative f =&gt; Bool -&gt; f () -&gt; f ()
</span><a href="GHC.Base.html#when"><span class="hs-identifier hs-var">when</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558424"><span class="hs-identifier hs-var">null_terminate</span></a></span><span> </span><span class="annot"><span class="annottext">(IO () -&gt; IO ()) -&gt; IO () -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8 -&gt; Int -&gt; Word8 -&gt; IO ()
forall a. Storable a =&gt; Ptr a -&gt; Int -&gt; a -&gt; IO ()
</span><a href="Foreign.Storable.html#pokeElemOff"><span class="hs-identifier hs-var">pokeElemOff</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558421"><span class="hs-identifier hs-var">to_ptr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Buffer Word8 -&gt; Int
forall e. Buffer e -&gt; Int
</span><a href="GHC.IO.Buffer.html#bufR"><span class="hs-identifier hs-var hs-var">bufR</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558425"><span class="hs-identifier hs-var">to_buf</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Word8
</span><span class="hs-number">0</span></span><span>
</span><span id="line-251"></span><span>    </span><span class="annot"><span class="annottext">CStringLen -&gt; IO r
</span><a href="#local-6989586621679558423"><span class="hs-identifier hs-var">act</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Ptr Word8 -&gt; CString
forall a b. Ptr a -&gt; Ptr b
</span><a href="GHC.Ptr.html#castPtr"><span class="hs-identifier hs-var">castPtr</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558421"><span class="hs-identifier hs-var">to_ptr</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558422"><span class="hs-identifier hs-var">bytes</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-comment">-- NB: the length information is specified as being in *bytes*</span><span>
</span><span id="line-252"></span><span>
</span><span id="line-253"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="GHC.Foreign.html#newEncodedCString"><span class="hs-pragma hs-type">newEncodedCString</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-254"></span><span class="annot"><a href="GHC.Foreign.html#newEncodedCString"><span class="hs-identifier hs-type">newEncodedCString</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span>  </span><span class="hs-comment">-- ^ Encoding of CString to create</span><span>
</span><span id="line-255"></span><span>                  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span>          </span><span class="hs-comment">-- ^ Null-terminate?</span><span>
</span><span id="line-256"></span><span>                  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span>        </span><span class="hs-comment">-- ^ String to encode</span><span>
</span><span id="line-257"></span><span>                  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="GHC.Foreign.html#CStringLen"><span class="hs-identifier hs-type">CStringLen</span></a></span><span>
</span><span id="line-258"></span><span id="newEncodedCString"><span class="annot"><span class="annottext">newEncodedCString :: TextEncoding -&gt; Bool -&gt; String -&gt; IO CStringLen
</span><a href="GHC.Foreign.html#newEncodedCString"><span class="hs-identifier hs-var hs-var">newEncodedCString</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoding"><span class="hs-identifier hs-type">TextEncoding</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">mkTextEncoder :: ()
</span><a href="GHC.IO.Encoding.Types.html#mkTextEncoder"><span class="hs-identifier hs-var">mkTextEncoder</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621679558411"><span class="annot"><span class="annottext">IO (TextEncoder estate)
</span><a href="#local-6989586621679558411"><span class="hs-identifier hs-var">mk_encoder</span></a></span></span><span> </span><span class="hs-special">}</span><span class="hs-special">)</span><span> </span><span id="local-6989586621679558410"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558410"><span class="hs-identifier hs-var">null_terminate</span></a></span></span><span> </span><span id="local-6989586621679558409"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558409"><span class="hs-identifier hs-var">s</span></a></span></span><span>
</span><span id="line-259"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO (TextEncoder estate)
-&gt; (TextEncoder estate -&gt; IO ())
-&gt; (TextEncoder estate -&gt; IO CStringLen)
-&gt; IO CStringLen
forall a b c. IO a -&gt; (a -&gt; IO b) -&gt; (a -&gt; IO c) -&gt; IO c
</span><a href="GHC.IO.html#bracket"><span class="hs-identifier hs-var">bracket</span></a></span><span> </span><span class="annot"><span class="annottext">IO (TextEncoder estate)
</span><a href="#local-6989586621679558411"><span class="hs-identifier hs-var">mk_encoder</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoder estate -&gt; IO ()
forall from to state. BufferCodec from to state -&gt; IO ()
</span><a href="GHC.IO.Encoding.Types.html#close"><span class="hs-identifier hs-var hs-var">close</span></a></span><span> </span><span class="annot"><span class="annottext">((TextEncoder estate -&gt; IO CStringLen) -&gt; IO CStringLen)
-&gt; (TextEncoder estate -&gt; IO CStringLen) -&gt; IO CStringLen
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558408"><span class="annot"><span class="annottext">TextEncoder estate
</span><a href="#local-6989586621679558408"><span class="hs-identifier hs-var">encoder</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">String -&gt; (Int -&gt; Ptr Char -&gt; IO CStringLen) -&gt; IO CStringLen
forall a b. Storable a =&gt; [a] -&gt; (Int -&gt; Ptr a -&gt; IO b) -&gt; IO b
</span><a href="Foreign.Marshal.Array.html#withArrayLen"><span class="hs-identifier hs-var">withArrayLen</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679558409"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="annot"><span class="annottext">((Int -&gt; Ptr Char -&gt; IO CStringLen) -&gt; IO CStringLen)
-&gt; (Int -&gt; Ptr Char -&gt; IO CStringLen) -&gt; IO CStringLen
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679558407"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558407"><span class="hs-identifier hs-var">sz</span></a></span></span><span> </span><span id="local-6989586621679558406"><span class="annot"><span class="annottext">Ptr Char
</span><a href="#local-6989586621679558406"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-260"></span><span>      </span><span id="local-6989586621679558405"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558405"><span class="hs-identifier hs-var">from</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(RawBuffer Char -&gt; CharBuffer)
-&gt; IO (RawBuffer Char) -&gt; IO CharBuffer
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679558404"><span class="annot"><span class="annottext">RawBuffer Char
</span><a href="#local-6989586621679558404"><span class="hs-identifier hs-var">fp</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Int -&gt; CharBuffer -&gt; CharBuffer
forall e. Int -&gt; Buffer e -&gt; Buffer e
</span><a href="GHC.IO.Buffer.html#bufferAdd"><span class="hs-identifier hs-var">bufferAdd</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558407"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">RawBuffer Char -&gt; Int -&gt; BufferState -&gt; CharBuffer
forall e. RawBuffer e -&gt; Int -&gt; BufferState -&gt; Buffer e
</span><a href="GHC.IO.Buffer.html#emptyBuffer"><span class="hs-identifier hs-var">emptyBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">RawBuffer Char
</span><a href="#local-6989586621679558404"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558407"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="annot"><span class="annottext">BufferState
</span><a href="GHC.IO.Buffer.html#ReadBuffer"><span class="hs-identifier hs-var">ReadBuffer</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">(IO (RawBuffer Char) -&gt; IO CharBuffer)
-&gt; IO (RawBuffer Char) -&gt; IO CharBuffer
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Char -&gt; IO (RawBuffer Char)
forall a. Ptr a -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier hs-var">newForeignPtr_</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Char
</span><a href="#local-6989586621679558406"><span class="hs-identifier hs-var">p</span></a></span><span>
</span><span id="line-261"></span><span>
</span><span id="line-262"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679558394"><span class="annot"><span class="annottext">go :: t -&gt; Ptr Word8 -&gt; Int -&gt; IO CStringLen
</span><a href="#local-6989586621679558394"><span class="hs-identifier hs-var hs-var">go</span></a></span></span><span> </span><span class="hs-glyph">!</span><span id="local-6989586621679558393"><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558393"><span class="hs-identifier hs-var">iteration</span></a></span></span><span> </span><span id="local-6989586621679558392"><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558392"><span class="hs-identifier hs-var">to_p</span></a></span></span><span> </span><span id="local-6989586621679558391"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558391"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-263"></span><span>           </span><span class="annot"><span class="annottext">String -&gt; IO ()
</span><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-var">putDebugMsg</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;newEncodedCString: &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; String
forall a. Show a =&gt; a -&gt; String
</span><a href="GHC.Show.html#show"><span class="hs-identifier hs-var">show</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558393"><span class="hs-identifier hs-var">iteration</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-264"></span><span>           </span><span id="local-6989586621679558390"><span class="annot"><span class="annottext">Maybe (Buffer Word8)
</span><a href="#local-6989586621679558390"><span class="hs-identifier hs-var">mb_res</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TextEncoder estate
-&gt; Bool
-&gt; CharBuffer
-&gt; Ptr Word8
-&gt; Int
-&gt; IO (Maybe (Buffer Word8))
forall dstate.
TextEncoder dstate
-&gt; Bool
-&gt; CharBuffer
-&gt; Ptr Word8
-&gt; Int
-&gt; IO (Maybe (Buffer Word8))
</span><a href="GHC.Foreign.html#tryFillBuffer"><span class="hs-identifier hs-var">tryFillBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoder estate
</span><a href="#local-6989586621679558408"><span class="hs-identifier hs-var">encoder</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558410"><span class="hs-identifier hs-var">null_terminate</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558405"><span class="hs-identifier hs-var">from</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558392"><span class="hs-identifier hs-var">to_p</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558391"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span>
</span><span id="line-265"></span><span>           </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Maybe (Buffer Word8)
</span><a href="#local-6989586621679558390"><span class="hs-identifier hs-var">mb_res</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-266"></span><span>             </span><span class="annot"><span class="annottext">Maybe (Buffer Word8)
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-267"></span><span>                 </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679558387"><span class="annot"><span class="annottext">to_sz_bytes' :: Int
</span><a href="#local-6989586621679558387"><span class="hs-identifier hs-var hs-var">to_sz_bytes'</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558391"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2A"><span class="hs-operator hs-var">*</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">2</span></span><span>
</span><span id="line-268"></span><span>                 </span><span id="local-6989586621679558386"><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558386"><span class="hs-identifier hs-var">to_p'</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Ptr Word8 -&gt; Int -&gt; IO (Ptr Word8)
forall a. Ptr a -&gt; Int -&gt; IO (Ptr a)
</span><a href="Foreign.Marshal.Alloc.html#reallocBytes"><span class="hs-identifier hs-var">reallocBytes</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558392"><span class="hs-identifier hs-var">to_p</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558387"><span class="hs-identifier hs-var">to_sz_bytes'</span></a></span><span>
</span><span id="line-269"></span><span>                 </span><span class="annot"><span class="annottext">t -&gt; Ptr Word8 -&gt; Int -&gt; IO CStringLen
</span><a href="#local-6989586621679558394"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">t
</span><a href="#local-6989586621679558393"><span class="hs-identifier hs-var">iteration</span></a></span><span> </span><span class="annot"><span class="annottext">t -&gt; t -&gt; t
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">t
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558386"><span class="hs-identifier hs-var">to_p'</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558387"><span class="hs-identifier hs-var">to_sz_bytes'</span></a></span><span>
</span><span id="line-270"></span><span>             </span><span class="annot"><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-type">Just</span></a></span><span> </span><span id="local-6989586621679558384"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558384"><span class="hs-identifier hs-var">to_buf</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Buffer Word8
-&gt; Bool -&gt; (CStringLen -&gt; IO CStringLen) -&gt; IO CStringLen
forall r. Buffer Word8 -&gt; Bool -&gt; (CStringLen -&gt; IO r) -&gt; IO r
</span><a href="GHC.Foreign.html#withCStringBuffer"><span class="hs-identifier hs-var">withCStringBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558384"><span class="hs-identifier hs-var">to_buf</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558410"><span class="hs-identifier hs-var">null_terminate</span></a></span><span> </span><span class="annot"><span class="annottext">CStringLen -&gt; IO CStringLen
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span>
</span><span id="line-271"></span><span>
</span><span id="line-272"></span><span>      </span><span class="hs-comment">-- If the input string is ASCII, this value will ensure we only allocate once</span><span>
</span><span id="line-273"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679558380"><span class="annot"><span class="annottext">to_sz_bytes :: Int
</span><a href="#local-6989586621679558380"><span class="hs-identifier hs-var hs-var">to_sz_bytes</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="GHC.Foreign.html#cCharSize"><span class="hs-identifier hs-var">cCharSize</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2A"><span class="hs-operator hs-var">*</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558407"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span>
</span><span id="line-274"></span><span>      </span><span id="local-6989586621679558379"><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558379"><span class="hs-identifier hs-var">to_p</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Int -&gt; IO (Ptr Word8)
forall a. Int -&gt; IO (Ptr a)
</span><a href="Foreign.Marshal.Alloc.html#mallocBytes"><span class="hs-identifier hs-var">mallocBytes</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558380"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span>
</span><span id="line-275"></span><span>      </span><span class="annot"><span class="annottext">Int -&gt; Ptr Word8 -&gt; Int -&gt; IO CStringLen
forall {t}.
(Show t, Num t) =&gt;
t -&gt; Ptr Word8 -&gt; Int -&gt; IO CStringLen
</span><a href="#local-6989586621679558394"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558379"><span class="hs-identifier hs-var">to_p</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558380"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span>
</span><span id="line-276"></span><span>
</span><span id="line-277"></span><span>
</span><span id="line-278"></span><span id="local-6989586621679558625"><span class="annot"><a href="GHC.Foreign.html#tryFillBuffer"><span class="hs-identifier hs-type">tryFillBuffer</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IO.Encoding.Types.html#TextEncoder"><span class="hs-identifier hs-type">TextEncoder</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679558625"><span class="hs-identifier hs-type">dstate</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.IO.Buffer.html#Buffer"><span class="hs-identifier hs-type">Buffer</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Char"><span class="hs-identifier hs-type">Char</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="GHC.Word.html#Word8"><span class="hs-identifier hs-type">Word8</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span>
</span><span id="line-279"></span><span>                    </span><span class="hs-glyph">-&gt;</span><span>  </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IO.Buffer.html#Buffer"><span class="hs-identifier hs-type">Buffer</span></a></span><span> </span><span class="annot"><a href="GHC.Word.html#Word8"><span class="hs-identifier hs-type">Word8</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span></span><span>
</span><span id="line-280"></span><span id="tryFillBuffer"><span class="annot"><span class="annottext">tryFillBuffer :: forall dstate.
TextEncoder dstate
-&gt; Bool
-&gt; CharBuffer
-&gt; Ptr Word8
-&gt; Int
-&gt; IO (Maybe (Buffer Word8))
</span><a href="GHC.Foreign.html#tryFillBuffer"><span class="hs-identifier hs-var hs-var">tryFillBuffer</span></a></span></span><span> </span><span id="local-6989586621679558374"><span class="annot"><span class="annottext">TextEncoder dstate
</span><a href="#local-6989586621679558374"><span class="hs-identifier hs-var">encoder</span></a></span></span><span> </span><span id="local-6989586621679558373"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558373"><span class="hs-identifier hs-var">null_terminate</span></a></span></span><span> </span><span id="local-6989586621679558372"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558372"><span class="hs-identifier hs-var">from0</span></a></span></span><span> </span><span id="local-6989586621679558371"><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558371"><span class="hs-identifier hs-var">to_p</span></a></span></span><span> </span><span id="local-6989586621679558370"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558370"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-281"></span><span>    </span><span id="local-6989586621679558369"><span class="annot"><span class="annottext">RawBuffer Word8
</span><a href="#local-6989586621679558369"><span class="hs-identifier hs-var">to_fp</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Ptr Word8 -&gt; IO (RawBuffer Word8)
forall a. Ptr a -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier hs-var">newForeignPtr_</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr Word8
</span><a href="#local-6989586621679558371"><span class="hs-identifier hs-var">to_p</span></a></span><span>
</span><span id="line-282"></span><span>    </span><span class="annot"><span class="annottext">Int -&gt; (CharBuffer, Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
forall {a}.
(Show a, Num a) =&gt;
a -&gt; (CharBuffer, Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
</span><a href="#local-6989586621679558368"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558372"><span class="hs-identifier hs-var">from0</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">RawBuffer Word8 -&gt; Int -&gt; BufferState -&gt; Buffer Word8
forall e. RawBuffer e -&gt; Int -&gt; BufferState -&gt; Buffer e
</span><a href="GHC.IO.Buffer.html#emptyBuffer"><span class="hs-identifier hs-var">emptyBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">RawBuffer Word8
</span><a href="#local-6989586621679558369"><span class="hs-identifier hs-var">to_fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679558370"><span class="hs-identifier hs-var">to_sz_bytes</span></a></span><span> </span><span class="annot"><span class="annottext">BufferState
</span><a href="GHC.IO.Buffer.html#WriteBuffer"><span class="hs-identifier hs-var">WriteBuffer</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-283"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-284"></span><span>    </span><span id="local-6989586621679558368"><span class="annot"><span class="annottext">go :: a -&gt; (CharBuffer, Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
</span><a href="#local-6989586621679558368"><span class="hs-identifier hs-var hs-var">go</span></a></span></span><span> </span><span class="hs-glyph">!</span><span id="local-6989586621679558349"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679558349"><span class="hs-identifier hs-var">iteration</span></a></span></span><span> </span><span class="hs-special">(</span><span id="local-6989586621679558348"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558348"><span class="hs-identifier hs-var">from</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558347"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558347"><span class="hs-identifier hs-var">to</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-285"></span><span>      </span><span class="hs-special">(</span><span id="local-6989586621679558346"><span class="annot"><span class="annottext">CodingProgress
</span><a href="#local-6989586621679558346"><span class="hs-identifier hs-var">why</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558345"><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558345"><span class="hs-identifier hs-var">from'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679558344"><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558344"><span class="hs-identifier hs-var">to'</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TextEncoder dstate -&gt; CodeBuffer Char Word8
forall from to state.
BufferCodec from to state -&gt; CodeBuffer from to
</span><a href="GHC.IO.Encoding.Types.html#encode"><span class="hs-identifier hs-var hs-var">encode</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoder dstate
</span><a href="#local-6989586621679558374"><span class="hs-identifier hs-var">encoder</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558348"><span class="hs-identifier hs-var">from</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558347"><span class="hs-identifier hs-var">to</span></a></span><span>
</span><span id="line-286"></span><span>      </span><span class="annot"><span class="annottext">String -&gt; IO ()
</span><a href="GHC.Foreign.html#putDebugMsg"><span class="hs-identifier hs-var">putDebugMsg</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;tryFillBufferAndCall: &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; String
forall a. Show a =&gt; a -&gt; String
</span><a href="GHC.Show.html#show"><span class="hs-identifier hs-var">show</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679558349"><span class="hs-identifier hs-var">iteration</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">CodingProgress -&gt; String
forall a. Show a =&gt; a -&gt; String
</span><a href="GHC.Show.html#show"><span class="hs-identifier hs-var">show</span></a></span><span> </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="#local-6989586621679558346"><span class="hs-identifier hs-var">why</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558348"><span class="hs-identifier hs-var">from</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot; &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; String -&gt; String
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer -&gt; String
forall a. Buffer a -&gt; String
</span><a href="GHC.IO.Buffer.html#summaryBuffer"><span class="hs-identifier hs-var">summaryBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558345"><span class="hs-identifier hs-var">from'</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-287"></span><span>      </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">CharBuffer -&gt; Bool
forall e. Buffer e -&gt; Bool
</span><a href="GHC.IO.Buffer.html#isEmptyBuffer"><span class="hs-identifier hs-var">isEmptyBuffer</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558345"><span class="hs-identifier hs-var">from'</span></a></span><span>
</span><span id="line-288"></span><span>       </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679558373"><span class="hs-identifier hs-var">null_terminate</span></a></span><span> </span><span class="annot"><span class="annottext">Bool -&gt; Bool -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%26%26"><span class="hs-operator hs-var">&amp;&amp;</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8 -&gt; Int
forall e. Buffer e -&gt; Int
</span><a href="GHC.IO.Buffer.html#bufferAvailable"><span class="hs-identifier hs-var">bufferAvailable</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558344"><span class="hs-identifier hs-var">to'</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Eq a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3D%3D"><span class="hs-operator hs-var">==</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span>
</span><span id="line-289"></span><span>             </span><span class="hs-keyword">then</span><span> </span><span class="annot"><span class="annottext">Maybe (Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">Maybe (Buffer Word8)
forall a. Maybe a
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span> </span><span class="hs-comment">-- We had enough for the string but not the terminator: ask the caller for more buffer</span><span>
</span><span id="line-290"></span><span>             </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">Maybe (Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Buffer Word8 -&gt; Maybe (Buffer Word8)
forall a. a -&gt; Maybe a
</span><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558344"><span class="hs-identifier hs-var">to'</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-291"></span><span>       </span><span class="hs-keyword">else</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="#local-6989586621679558346"><span class="hs-identifier hs-var">why</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-comment">-- We didn't consume all of the input</span><span>
</span><span id="line-292"></span><span>              </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="GHC.IO.Encoding.Types.html#InputUnderflow"><span class="hs-identifier hs-var">InputUnderflow</span></a></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">TextEncoder dstate
-&gt; CharBuffer -&gt; Buffer Word8 -&gt; IO (CharBuffer, Buffer Word8)
forall from to state.
BufferCodec from to state
-&gt; Buffer from -&gt; Buffer to -&gt; IO (Buffer from, Buffer to)
</span><a href="GHC.IO.Encoding.Types.html#recover"><span class="hs-identifier hs-var hs-var">recover</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoder dstate
</span><a href="#local-6989586621679558374"><span class="hs-identifier hs-var">encoder</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558345"><span class="hs-identifier hs-var">from'</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558344"><span class="hs-identifier hs-var">to'</span></a></span><span> </span><span class="annot"><span class="annottext">IO (CharBuffer, Buffer Word8)
-&gt; ((CharBuffer, Buffer Word8) -&gt; IO (Maybe (Buffer Word8)))
-&gt; IO (Maybe (Buffer Word8))
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; (CharBuffer, Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
</span><a href="#local-6989586621679558368"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679558349"><span class="hs-identifier hs-var">iteration</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; a -&gt; a
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span> </span><span class="hs-comment">-- These conditions are equally bad</span><span>
</span><span id="line-293"></span><span>              </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="GHC.IO.Encoding.Types.html#InvalidSequence"><span class="hs-identifier hs-var">InvalidSequence</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">TextEncoder dstate
-&gt; CharBuffer -&gt; Buffer Word8 -&gt; IO (CharBuffer, Buffer Word8)
forall from to state.
BufferCodec from to state
-&gt; Buffer from -&gt; Buffer to -&gt; IO (Buffer from, Buffer to)
</span><a href="GHC.IO.Encoding.Types.html#recover"><span class="hs-identifier hs-var hs-var">recover</span></a></span><span> </span><span class="annot"><span class="annottext">TextEncoder dstate
</span><a href="#local-6989586621679558374"><span class="hs-identifier hs-var">encoder</span></a></span><span> </span><span class="annot"><span class="annottext">CharBuffer
</span><a href="#local-6989586621679558345"><span class="hs-identifier hs-var">from'</span></a></span><span> </span><span class="annot"><span class="annottext">Buffer Word8
</span><a href="#local-6989586621679558344"><span class="hs-identifier hs-var">to'</span></a></span><span> </span><span class="annot"><span class="annottext">IO (CharBuffer, Buffer Word8)
-&gt; ((CharBuffer, Buffer Word8) -&gt; IO (Maybe (Buffer Word8)))
-&gt; IO (Maybe (Buffer Word8))
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; (CharBuffer, Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
</span><a href="#local-6989586621679558368"><span class="hs-identifier hs-var">go</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679558349"><span class="hs-identifier hs-var">iteration</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; a -&gt; a
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span> </span><span class="hs-comment">-- since the input was truncated/invalid</span><span>
</span><span id="line-294"></span><span>              </span><span class="annot"><span class="annottext">CodingProgress
</span><a href="GHC.IO.Encoding.Types.html#OutputUnderflow"><span class="hs-identifier hs-var">OutputUnderflow</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Maybe (Buffer Word8) -&gt; IO (Maybe (Buffer Word8))
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">Maybe (Buffer Word8)
forall a. Maybe a
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span> </span><span class="hs-comment">-- Oops, out of buffer during decoding: ask the caller for more</span><span>
</span><span id="line-295"></span><span class="hs-comment">{-
Note [Check *before* fill in withEncodedCString]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It's very important that the size check and readjustment peformed by tryFillBuffer
happens before the continuation is called. The size check is the part which can
fail, the call to the continuation never fails and so the caller should respond
first to the size check failing and *then* call the continuation. Making this evident
to the compiler avoids historic space leaks.

In a previous interation of this code we had a pattern that, somewhat simplified,
looked like this:

go :: State -&gt; (State -&gt; IO a) -&gt; IO a
go state action =
    case tryFillBufferAndCall state action of
        Left state' -&gt; go state' action
        Right result -&gt; result

`tryFillBufferAndCall` performed some checks, and then we either called action,
or we modified the state and tried again.
This went wrong because `action` can be a function closure containing a reference to
a lazy data structure. If we call action directly, without retaining any references
to action, that is fine. The data structure is consumed as it is produced and we operate
in constant space.

However the failure branch `go state' action` *does* capture a reference to action.
This went wrong because the reference to action in the failure branch only becomes
unreachable *after* action returns. This means we keep alive the function closure
for `action` until `action` returns. Which in turn keeps alive the *whole* lazy list
via `action` until the action has fully run.
This went wrong in #20107, where the continuation kept an entire lazy bytestring alive
rather than allowing it to be incrementaly consumed and collected.
-}</span><span>
</span><span id="line-329"></span><span>
</span><span id="line-330"></span></pre></body></html>